#import "OmRegisterVCCharacterView.h"
#import "LoginApiGetCharacterModel.h"
@interface OmRegisterVCCharacterView ()
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) JCTagListView *tagsView;
@property (nonatomic, strong) NSArray<LoginApiGetCharacterModel *> *models;
@end
@implementation OmRegisterVCCharacterView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self configSelf];
        [self allocateComponents];
        [self layoutComponents];
        [self refreshSelf];
    }
    return self;
}
- (void)configSelf {
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(WINDOW_WIDTH));
        make.height.equalTo(@(RPX(160)));
    }];
}
- (void)allocateComponents {
    self.titleLabel = [[UILabel alloc] init].fp_text(ASLocalizedString(@"请添加性格")).fp_font(RPXFont(15)).fp_textColor([UIColor blackColor]);
    [self addSubview:self.titleLabel];
    self.tagsView = [[JCTagListView alloc] init];
    self.tagsView.canSelectTags = YES;
    self.tagsView.tagSelectedTextColor = HexadecimalColor(@"#Ffffff");
    self.tagsView.tagSelectedBackgroundColor = HexadecimalColor(@"#F07896");
    self.tagsView.tagSelectedStrokeColor = HexadecimalColor(@"#F07896");
    [self addSubview:self.tagsView];
}
- (void)layoutComponents {
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(RPX(30));
        make.top.equalTo(self).offset(RPX(24));
    }];
    [self.tagsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(RPX(45));
        make.left.bottom.equalTo(self).offset(RPX(30));
        make.right.equalTo(self).offset(RPX(-30));
    }];
}
- (void)refreshSelf {
    NSString *url = [[CYURLUtils sharedCYURLUtils] makeURLWithC:@"login_api" A:@"get_character"];
    [CYNET POST:url parameters:@{@"dfs":@"sdf"} responseCache:^(id responseObject) {
    } success:^(id responseObject) {
        self.models = [LoginApiGetCharacterModel mj_objectArrayWithKeyValuesArray:responseObject[@"character"]];
        self.tagsView.tags = [self tagsArrayFromModels];
        [self.tagsView.collectionView reloadData];
    } failure:^(NSString *error) {
    } hasCache:NO];
}
- (NSMutableArray *)tagsArrayFromModels {
    NSMutableArray *array = [NSMutableArray array];
    for (LoginApiGetCharacterModel *model in self.models) {
        [array addObject:isCurrentLanguageChinese?model.z_name:model.e_name];
    }
    return array;
}
- (NSMutableArray *)characters {
    NSMutableArray *characters = [NSMutableArray array];
    for (NSString *tag in self.tagsView.selectedTags) {
        for (LoginApiGetCharacterModel *model in self.models) {
            if ([isCurrentLanguageChinese?model.z_name:model.e_name isEqualToString:tag]) {
                [characters addObject:@(model.ID)];
            }
        }
    }
    return characters;
}
@end
